#include "../include/WebPage.h"
#include "../include/SplitTool.h"
#include "../include/Configuration.h"

#include <stdlib.h>

// SplitTool *_psplitTool = new SplitTool();

WebPage::WebPage(string &doc, Configuration *pconf) 
// : _psplitTool(psplitTool)
// : _pconf(pconf)
: _doc(doc)
{
    size_t contentBegin = _doc.find("<content>");
    size_t contentEnd = _doc.find("</content>");
    _docContent = _doc.substr(contentBegin + 9, contentEnd - contentBegin - 9);

    size_t titleBegin = _doc.find("<title>");
    size_t titleEnd = _doc.find("</title>");
    _docTitle = _doc.substr(titleBegin + 7, titleEnd - titleBegin - 7);
}

WebPage::~WebPage() {
}

int WebPage::getDocId() {
    size_t idBegin = _doc.find("<docid>");
    size_t idEnd = _doc.find("</docid>");
    _docId = atoi((_doc.substr(idBegin + 7, idEnd - idBegin - 7)).c_str());

}

string WebPage::getDoc() {
    return _doc;
}

map<string, int> &WebPage::getWordsMap() {
    set<string> mySet = _pconf->getStopWordList();
    vector<string> words = _staticsplitTool->cut(_docContent);
    for (auto it = words.begin(); it != words.end(); ++it) {
        if (mySet.count(*it)) {
            continue;
        }
        ++_wordsMap[*it];
    }
    return _wordsMap;
}

// uint64_t WebPage::getSimhash() {
//     size_t Top_N = 5;
//     uint64_t u64 = 0;
//     vector<pair<string, double>> res;
//     simhasher.extract(_doc, res, Top_N);
//     simhasher.make(_doc, Top_N, u64);
//     return u64;
// }
bool operator==(const WebPage &lhs, const WebPage &rhs) {
    size_t TOP_K = 5;
    uint64_t lu64 = 0, ru64 = 0;
    vector<pair<string ,double>> lres, rres;
    simhasher.extract(lhs._doc, lres, TOP_K);
    simhasher.make(lhs._doc, TOP_K, lu64);
    simhasher.extract(rhs._doc, rres, TOP_K);
    simhasher.make(rhs._doc, TOP_K, ru64);
    return Simhasher::isEqual(lu64, ru64);
    // uint64_t u64 = lhs.getSimhash();
    // return Simhasher::isEqual(lhs.getSimhash(), rhs.getSimhash());
}

bool operator<(const WebPage &lhs, const WebPage &rhs) {
    //return lhs._docId < rhs._docId;

    size_t TOP_K = 5;
    uint64_t lu64 = 0, ru64 = 0;
    vector<pair<string ,double>> lres, rres;
    simhasher.extract(lhs._doc, lres, TOP_K);
    simhasher.make(lhs._doc, TOP_K, lu64);
    simhasher.extract(rhs._doc, rres, TOP_K);
    simhasher.make(rhs._doc, TOP_K, ru64);

    ostringstream oss1, oss2;
    for(int i = 63; i>=0; i--)
    {
        oss1 << ( (lu64>>i) & 1);//与1做位操作，前面位数均为0
        oss2 << ( (ru64>>i) & 1);//与1做位操作，前面位数均为0
        //cout<<( (n>>i) & 1);//输出二进制
    }
    return oss1.str() < oss2.str();
}


void WebPage::processDoc(const string &doc, Configuration &conf, SplitTool &splitool) {

}

void WebPage::calcTopK(vector<string> &wordsVec, int k, set<string> &stopWordList) {

}
